package com.bookshop.servlet;

import com.alibaba.fastjson.JSON;
import com.bookshop.entity.ResponseResult;
import com.bookshop.service.CategoryService;
import com.bookshop.service.impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/DeleteCategory")
public class DeleteCategoryServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        String id = req.getParameter("id");
        CategoryService categoryService=new CategoryServiceImpl();
        int result = categoryService.deleteById(Integer.valueOf(id));
        ResponseResult outResult=null;
        if (result>0){
            outResult=new ResponseResult(200,"删除成功");
        }else if(result==-1){
            outResult=new ResponseResult(401,"删除异常");
        }else{
            outResult=new ResponseResult(400,"删除失败！");
        }
        String outJson = JSON.toJSONString(outResult);
        out.print(outJson);
        out.flush();
        out.close();




    }
}
